/*
 * Copyright (C) 2008,2009,2010,2011  Paulo Cesar Pereira de Andrade.
 *
 * This is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * Authors:
 *	Paulo Cesar Pereira de Andrade
 */

#include "exl.h"

#define r0			thread_self->r0
#define ss			thread_self->ess
#define bp			thread_self->ebp
#define sp			thread_self->esp
#define obj			thread_self->obj
#define ret			thread_self->ret
#define flt			thread_self->flt

/*
 * Implementation
 */
void
evm_r_ne(evalue_t *v)
{
    r0.t = t_int;
    switch (v->t) {
	case t_void:
	    r0.v.i = !mpfr_zero_p(r0.v.mpr);
	    break;
	case t_int:
	    empr_set_i(thr_rr0, v->v.i);
	    r0.v.i = !mpfr_equal_p(thr_rr0, r0.v.mpr);
	    break;
	case t_float:
	    mpfr_set_d(thr_rr0, v->v.d, thr_rnd);
	    r0.v.i = !mpfr_equal_p(thr_rr0, r0.v.mpr);
	    break;
	case t_mpz:		case t_shz:
	    mpfr_set_z(thr_rr0, v->v.mpz, thr_rnd);
	    r0.v.i = !mpfr_equal_p(thr_rr0, r0.v.mpr);
	    break;
	case t_mpq:		case t_shq:
	    mpfr_set_q(thr_rr0, v->v.mpq, thr_rnd);
	    r0.v.i = !mpfr_equal_p(thr_rr0, r0.v.mpr);
	    break;
	case t_shr:	case t_mpr:
	    r0.v.i = !mpfr_equal_p(v->v.mpr, r0.v.mpr);
	    break;
	default:
	    r0.v.i = 1;
	    break;
    }
}

void
evm_r_lt(evalue_t *v)
{
    r0.t = t_int;
    switch (v->t) {
	case t_void:
	    mpfr_set_ui(thr_rr0, 0, thr_rnd);
	    r0.v.i = mpfr_less_p(thr_rr0, r0.v.mpr);
	    break;
	case t_int:
	    empr_set_i(thr_rr0, v->v.i);
	    r0.v.i = mpfr_less_p(thr_rr0, r0.v.mpr);
	    break;
	case t_float:
	    mpfr_set_d(thr_rr0, v->v.d, thr_rnd);
	    r0.v.i = mpfr_less_p(thr_rr0, r0.v.mpr);
	    break;
	case t_mpz:		case t_shz:
	    mpfr_set_z(thr_rr0, v->v.mpz, thr_rnd);
	    r0.v.i = mpfr_less_p(thr_rr0, r0.v.mpr);
	    break;
	case t_mpq:		case t_shq:
	    mpfr_set_q(thr_rr0, v->v.mpq, thr_rnd);
	    r0.v.i = mpfr_less_p(thr_rr0, r0.v.mpr);
	    break;
	case t_mpr:		case t_shr:
	    r0.v.i = mpfr_less_p(v->v.mpr, r0.v.mpr);
	    break;
	default:
	    evm_raise(except_not_a_real_number);
    }
}

void
evm_r_le(evalue_t *v)
{
    r0.t = t_int;
    switch (v->t) {
	case t_void:
	    mpfr_set_ui(thr_rr0, 0, thr_rnd);
	    r0.v.i = mpfr_lessequal_p(thr_rr0, r0.v.mpr);
	    break;
	case t_int:
	    empr_set_i(thr_rr0, v->v.i);
	    r0.v.i = mpfr_lessequal_p(thr_rr0, r0.v.mpr);
	    break;
	case t_float:
	    mpfr_set_d(thr_rr0, v->v.d, thr_rnd);
	    r0.v.i = mpfr_lessequal_p(thr_rr0, r0.v.mpr);
	    break;
	case t_mpz:		case t_shz:
	    mpfr_set_z(thr_rr0, v->v.mpz, thr_rnd);
	    r0.v.i = mpfr_lessequal_p(thr_rr0, r0.v.mpr);
	    break;
	case t_mpq:		case t_shq:
	    mpfr_set_q(thr_rr0, v->v.mpq, thr_rnd);
	    r0.v.i = mpfr_lessequal_p(thr_rr0, r0.v.mpr);
	    break;
	case t_mpr:		case t_shr:
	    r0.v.i = mpfr_lessequal_p(v->v.mpr, r0.v.mpr);
	    break;
	default:
	    evm_raise(except_not_a_real_number);
    }
}

void
evm_r_eq(evalue_t *v)
{
    r0.t = t_int;
    switch (v->t) {
	case t_void:
	    r0.v.i = mpfr_zero_p(r0.v.mpr);
	    break;
	case t_int:
	    empr_set_i(thr_rr0, v->v.i);
	    r0.v.i = mpfr_equal_p(thr_rr0, r0.v.mpr);
	    break;
	case t_float:
	    mpfr_set_d(thr_rr0, v->v.d, thr_rnd);
	    r0.v.i = mpfr_equal_p(thr_rr0, r0.v.mpr);
	    break;
	case t_mpz:		case t_shz:
	    mpfr_set_z(thr_rr0, v->v.mpz, thr_rnd);
	    r0.v.i = mpfr_equal_p(thr_rr0, r0.v.mpr);
	    break;
	case t_mpq:		case t_shq:
	    mpfr_set_q(thr_rr0, v->v.mpq, thr_rnd);
	    r0.v.i = mpfr_equal_p(thr_rr0, r0.v.mpr);
	    break;
	case t_mpr:		case t_shr:
	    r0.v.i = mpfr_equal_p(v->v.mpr, r0.v.mpr);
	    break;
	default:
	    r0.v.i = 0;
	    break;
    }
}

void
evm_r_ge(evalue_t *v)
{
    r0.t = t_int;
    switch (v->t) {
	case t_void:
	    mpfr_set_ui(thr_rr0, 0, thr_rnd);
	    r0.v.i = mpfr_greaterequal_p(thr_rr0, r0.v.mpr);
	    break;
	case t_int:
	    empr_set_i(thr_rr0, v->v.i);
	    r0.v.i = mpfr_greaterequal_p(thr_rr0, r0.v.mpr);
	    break;
	case t_float:
	    mpfr_set_d(thr_rr0, v->v.d, thr_rnd);
	    r0.v.i = mpfr_greaterequal_p(thr_rr0, r0.v.mpr);
	    break;
	case t_mpz:		case t_shz:
	    mpfr_set_z(thr_rr0, v->v.mpz, thr_rnd);
	    r0.v.i = mpfr_greaterequal_p(thr_rr0, r0.v.mpr);
	    break;
	case t_mpq:		case t_shq:
	    mpfr_set_q(thr_rr0, v->v.mpq, thr_rnd);
	    r0.v.i = mpfr_greaterequal_p(thr_rr0, r0.v.mpr);
	    break;
	case t_mpr:		case t_shr:
	    r0.v.i = mpfr_greaterequal_p(v->v.mpr, r0.v.mpr);
	    break;
	default:
	    evm_raise(except_not_a_real_number);
    }
}

void
evm_r_gt(evalue_t *v)
{
    r0.t = t_int;
    switch (v->t) {
	case t_void:
	    mpfr_set_ui(thr_rr0, 0, thr_rnd);
	    r0.v.i = mpfr_greater_p(thr_rr0, r0.v.mpr);
	    break;
	case t_int:
	    empr_set_i(thr_rr0, v->v.i);
	    r0.v.i = mpfr_greater_p(thr_rr0, r0.v.mpr);
	    break;
	case t_float:
	    mpfr_set_d(thr_rr0, v->v.d, thr_rnd);
	    r0.v.i = mpfr_greater_p(thr_rr0, r0.v.mpr);
	    break;
	case t_mpz:		case t_shz:
	    mpfr_set_z(thr_rr0, v->v.mpz, thr_rnd);
	    r0.v.i = mpfr_greater_p(thr_rr0, r0.v.mpr);
	    break;
	case t_mpq:		case t_shq:
	    mpfr_set_q(thr_rr0, v->v.mpq, thr_rnd);
	    r0.v.i = mpfr_greater_p(thr_rr0, r0.v.mpr);
	    break;
	case t_mpr:		case t_shr:
	    r0.v.i = mpfr_greater_p(v->v.mpr, r0.v.mpr);
	    break;
	default:
	    evm_raise(except_not_a_real_number);
    }
}

void
evm_r_add(evalue_t *v)
{
    switch (v->t) {
	case t_void:
	    er_add_i_r(thr_rr, 0, r0.v.mpr);
	    chk_rr();
	    break;
	case t_int:
	    er_add_i_r(thr_rr, v->v.i, r0.v.mpr);
	    chk_rr();
	    break;
	case t_float:
	    er_add_d_r(thr_rr, v->v.d, r0.v.mpr);
	    chk_rr();
	    break;
	case t_mpz:		case t_shz:
	    er_add_z_r(thr_rr, v->v.mpz, r0.v.mpr);
	    chk_rr();
	    break;
	case t_mpq:		case t_shq:
	    er_add_q_r(thr_rr, v->v.mpq, r0.v.mpr);
	    chk_rr();
	    break;
	case t_mpr:		case t_shr:
	    er_add_r_r(thr_rr, v->v.mpr, r0.v.mpr);
	    chk_rr();
	    break;
	case t_cdd:
	    ecc_add_dd_r(thr_c, v->v.cdd, r0.v.mpr);
	    set_rr_chk_rrr();
	    break;
	case t_cqq:		case t_sqq:
	    ecc_add_qq_r(thr_c, v->v.cqq, r0.v.mpr);
	    set_rr_chk_rrr();
	    break;
	case t_mpc:		case t_shc:
	    ecc_add_cc_r(thr_c, v->v.mpc, r0.v.mpr);
	    set_rr_chk_rrr();
	    break;
	default:
	    if (!evector_type_p(v->t))
		evm_raise(except_not_a_number);
	    evector_add_mpr(v->v.o, r0.v.mpr, v->t & ~(t_const|t_vector));
	    r0.v.o = obj;
	    r0.t = eobject_type(obj);
	    break;
    }
}

void
evm_r_sub(evalue_t *v)
{
    switch (v->t) {
	case t_void:
	    er_sub_i_r(thr_rr, 0, r0.v.mpr);
	    chk_rr();
	    break;
	case t_int:
	    er_sub_i_r(thr_rr, v->v.i, r0.v.mpr);
	    chk_rr();
	    break;
	case t_float:
	    er_sub_d_r(thr_rr, v->v.d, r0.v.mpr);
	    chk_rr();
	    break;
	case t_mpz:		case t_shz:
	    er_sub_z_r(thr_rr, v->v.mpz, r0.v.mpr);
	    chk_rr();
	    break;
	case t_mpq:		case t_shq:
	    er_sub_q_r(thr_rr, v->v.mpq, r0.v.mpr);
	    chk_rr();
	    break;
	case t_mpr:		case t_shr:
	    er_sub_r_r(thr_rr, v->v.mpr, r0.v.mpr);
	    chk_rr();
	    break;
	case t_cdd:
	    ecc_sub_dd_r(thr_c, v->v.cdd, r0.v.mpr);
	    set_rr_chk_rrr();
	    break;
	case t_cqq:		case t_sqq:
	    ecc_sub_qq_r(thr_c, v->v.cqq, r0.v.mpr);
	    set_rr_chk_rrr();
	    break;
	case t_mpc:		case t_shc:
	    ecc_sub_cc_r(thr_c, v->v.mpc, r0.v.mpr);
	    set_rr_chk_rrr();
	    break;
	default:
	    if (!evector_type_p(v->t))
		evm_raise(except_not_a_number);
	    evector_sub_mpr(v->v.o, r0.v.mpr, v->t & ~(t_const|t_vector));
	    r0.v.o = obj;
	    r0.t = eobject_type(obj);
	    break;
    }
}

void
evm_r_mul(evalue_t *v)
{
    switch (v->t) {
	case t_void:
	    er_mul_i_r(thr_rr, 0, r0.v.mpr);
	    chk_rr();
	    break;
	case t_int:
	    er_mul_i_r(thr_rr, v->v.i, r0.v.mpr);
	    chk_rr();
	    break;
	case t_float:
	    er_mul_d_r(thr_rr, v->v.d, r0.v.mpr);
	    chk_rr();
	    break;
	case t_mpz:		case t_shz:
	    er_mul_z_r(thr_rr, v->v.mpz, r0.v.mpr);
	    chk_rr();
	    break;
	case t_mpq:		case t_shq:
	    er_mul_q_r(thr_rr, v->v.mpq, r0.v.mpr);
	    chk_rr();
	    break;
	case t_mpr:		case t_shr:
	    mpfr_mul(thr_rr, v->v.mpr, r0.v.mpr, thr_rnd);
	    chk_rr();
	    break;
	case t_cdd:
	    ecc_mul_dd_r(thr_c, v->v.cdd, r0.v.mpr);
	    set_rr_chk_rrr();
	    break;
	case t_cqq:		case t_sqq:
	    ecc_mul_qq_r(thr_c, v->v.cqq, r0.v.mpr);
	    set_rr_chk_rrr();
	    break;
	case t_mpc:		case t_shc:
	    ecc_mul_cc_r(thr_c, v->v.mpc, r0.v.mpr);
	    set_rr_chk_rrr();
	    break;
	default:
	    if (!evector_type_p(v->t))
		evm_raise(except_not_a_number);
	    evector_mul_mpr(v->v.o, r0.v.mpr, v->t & ~(t_const|t_vector));
	    r0.v.o = obj;
	    r0.t = eobject_type(obj);
	    break;
    }
}

void
evm_r_div(evalue_t *v)
{
    switch (v->t) {
	case t_void:
	    er_div_i_r(thr_rr, 0, r0.v.mpr);
	    chk_rr();
	    break;
	case t_int:
	    er_div_i_r(thr_rr, v->v.i, r0.v.mpr);
	    chk_rr();
	    break;
	case t_float:
	    er_div_d_r(thr_rr, v->v.d, r0.v.mpr);
	    chk_rr();
	    break;
	case t_mpz:		case t_shz:
	    er_div_z_r(thr_rr, v->v.mpz, r0.v.mpr);
	    chk_rr();
	    break;
	case t_mpq:		case t_shq:
	    er_div_q_r(thr_rr, v->v.mpq, r0.v.mpr);
	    chk_rr();
	    break;
	case t_mpr:		case t_shr:
	    er_div_r_r(thr_rr, v->v.mpr, r0.v.mpr);
	    chk_rr();
	    break;
	case t_cdd:
	    ecc_div_dd_r(thr_c, v->v.cdd, r0.v.mpr);
	    set_rr_chk_rrr();
	    break;
	case t_cqq:		case t_sqq:
	    ecc_div_qq_r(thr_c, v->v.cqq, r0.v.mpr);
	    set_rr_chk_rrr();
	    break;
	case t_mpc:		case t_shc:
	    ecc_div_cc_r(thr_c, v->v.mpc, r0.v.mpr);
	    set_rr_chk_rrr();
	    break;
	default:
	    if (!evector_type_p(v->t))
		evm_raise(except_not_a_number);
	    evector_div_mpr(v->v.o, r0.v.mpr, v->t & ~(t_const|t_vector));
	    r0.v.o = obj;
	    r0.t = eobject_type(obj);
	    break;
    }
}

void
evm_r_trunc2(evalue_t *v)
{
    switch (v->t) {
	case t_void:
	    /* result is zero or exception */
	    r0.t = t_int;
	    r0.v.i = ez_trunc_i_r(thr_zr, 0, r0.v.mpr);
	    break;
	case t_int:
	    r0.t = t_int;
	    if ((r0.v.i = ez_trunc_i_r(thr_zr, v->v.i, r0.v.mpr)) == MININT)
		set_rz();
	    break;
	case t_float:
	    r0.t = t_int;
	    if ((r0.v.i = ez_trunc_d_r(thr_zr, v->v.d, r0.v.mpr)) == MININT)
		set_rz();
	    break;
	case t_mpz:		case t_shz:
	    r0.t = t_int;
	    if ((r0.v.i = ez_trunc_z_r(thr_zr, v->v.mpz, r0.v.mpr)) == MININT)
		set_rz();
	    break;
	case t_mpq:		case t_shq:
	    r0.t = t_int;
	    if ((r0.v.i = ez_trunc_q_r(thr_zr, v->v.mpq, r0.v.mpr)) == MININT)
		set_rz();
	    break;
	case t_mpr:		case t_shr:
	    r0.t = t_int;
	    if ((r0.v.i = ez_trunc_r_r(thr_zr, v->v.mpr, r0.v.mpr)) == MININT)
		set_rz();
	    break;
	default:
	    if (!evector_type_p(v->t))
		evm_raise(except_not_a_real_number);
	    evector_trunc_mpr(v->v.o, r0.v.mpr, v->t & ~(t_const|t_vector));
	    r0.v.o = obj;
	    r0.t = eobject_type(obj);
	    break;
    }
}

void
evm_r_rem(evalue_t *v)
{
    switch (v->t) {
	case t_void:
	    er_rem_i_r(thr_rr, 0, r0.v.mpr);
	    break;
	case t_int:
	    er_rem_i_r(thr_rr, v->v.i, r0.v.mpr);
	    break;
	case t_float:
	    er_rem_d_r(thr_rr, v->v.d, r0.v.mpr);
	    break;
	case t_mpz:		case t_shz:
	    er_rem_z_r(thr_rr, v->v.mpz, r0.v.mpr);
	    break;
	case t_mpq:		case t_shq:
	    er_rem_q_r(thr_rr, v->v.mpq, r0.v.mpr);
	    break;
	case t_mpr:		case t_shr:
	    er_rem_r_r(thr_rr, v->v.mpr, r0.v.mpr);
	    break;
	default:
	    if (!evector_type_p(v->t))
		evm_raise(except_not_a_real_number);
	    evector_rem_mpr(v->v.o, r0.v.mpr, v->t & ~(t_const|t_vector));
	    r0.v.o = obj;
	    r0.t = eobject_type(obj);
	    return;
    }
    chk_rr();
}

void
evm_r_atan2(evalue_t *v)
{
    switch (v->t) {
	case t_void:
	    er_atan2_i_r(thr_rr, 0, r0.v.mpr);
	    chk_rr();
	    break;
	case t_int:
	    er_atan2_i_r(thr_rr, v->v.i, r0.v.mpr);
	    chk_rr();
	    break;
	case t_float:
	    er_atan2_d_r(thr_rr, v->v.d, r0.v.mpr);
	    chk_rr();
	    break;
	case t_mpz:		case t_shz:
	    er_atan2_z_r(thr_rr, v->v.mpz, r0.v.mpr);
	    chk_rr();
	    break;
	case t_mpq:		case t_shq:
	    er_atan2_q_r(thr_rr, v->v.mpq, r0.v.mpr);
	    chk_rr();
	    break;
	case t_mpr:		case t_shr:
	    er_atan2_r_r(thr_rr, v->v.mpr, r0.v.mpr);
	    chk_rr();
	    break;
	case t_cdd:
	    ecc_atan2_dd_r(thr_c, v->v.cdd, r0.v.mpr);
	    set_rr_chk_rrr();
	    break;
	case t_cqq:		case t_sqq:
	    ecc_atan2_qq_r(thr_c, v->v.cqq, r0.v.mpr);
	    set_rr_chk_rrr();
	    break;
	case t_mpc:		case t_shc:
	    ecc_atan2_cc_r(thr_c, v->v.mpc, r0.v.mpr);
	    set_rr_chk_rrr();
	    break;
	default:
	    if (!evector_type_p(v->t))
		evm_raise(except_not_a_number);
	    evector_atan2_mpr(v->v.o, r0.v.mpr, v->t & ~(t_const|t_vector));
	    r0.v.o = obj;
	    r0.t = eobject_type(obj);
	    break;
    }
}

void
evm_r_pow(evalue_t *v)
{
    switch (v->t) {
	case t_void:
	    er_pow_i_r(thr_rr, 0, r0.v.mpr);
	    chk_rr();
	    break;
	case t_int:
	    if (v->v.i < 0 &&
		mpfr_number_p(r0.v.mpr) && !mpfr_integer_p(r0.v.mpr)) {
		ecc_pow_i_r(thr_c, v->v.i, r0.v.mpr);
		set_rr_chk_rrr();
	    }
	    else {
		er_pow_i_r(thr_rr, v->v.i, r0.v.mpr);
		chk_rr();
	    }
	    break;
	case t_float:
	    if (finite(v->v.d) && v->v.d < 0.0 &&
		mpfr_number_p(r0.v.mpr) && !mpfr_integer_p(r0.v.mpr)) {
		ecc_pow_d_r(thr_c, v->v.d, r0.v.mpr);
		set_rr_chk_rrr();
	    }
	    else {
		er_pow_d_r(thr_rr, v->v.d, r0.v.mpr);
		chk_rr();
	    }
	    break;
	case t_mpz:		case t_shz:
	    if (mpz_sgn(v->v.mpz) < 0 &&
		mpfr_number_p(r0.v.mpr) && !mpfr_integer_p(r0.v.mpr)) {
		ecc_pow_z_r(thr_c, v->v.mpz, r0.v.mpr);
		set_rr_chk_rrr();
	    }
	    else {
		er_pow_z_r(thr_rr, v->v.mpz, r0.v.mpr);
		chk_rr();
	    }
	    break;
	case t_mpq:		case t_shq:
	    if (mpq_sgn(v->v.mpq) < 0 &&
		mpfr_number_p(r0.v.mpr) && !mpfr_integer_p(r0.v.mpr)) {
		ecc_pow_q_r(thr_c, v->v.mpq, r0.v.mpr);
		set_rr_chk_rrr();
	    }
	    else {
		er_pow_q_r(thr_rr, v->v.mpq, r0.v.mpr);
		chk_rr();
	    }
	    break;
	case t_mpr:		case t_shr:
	    if (mpfr_sgn(v->v.mpr) < 0 &&
		mpfr_number_p(r0.v.mpr) && !mpfr_integer_p(r0.v.mpr)) {
		ecc_pow_r_r(thr_c, v->v.mpr, r0.v.mpr);
		set_rr_chk_rrr();
	    }
	    else {
		er_pow_r_r(thr_rr, v->v.mpr, r0.v.mpr);
		chk_rr();
	    }
	    break;
	case t_cdd:
	    ecc_pow_dd_r(thr_c, v->v.cdd, r0.v.mpr);
	    set_rr_chk_rrr();
	    break;
	case t_cqq:		case t_sqq:
	    ecc_pow_qq_r(thr_c, v->v.cqq, r0.v.mpr);
	    set_rr_chk_rrr();
	    break;
	case t_mpc:		case t_shc:
	    ecc_pow_cc_r(thr_c, v->v.mpc, r0.v.mpr);
	    set_rr_chk_rrr();
	    break;
	default:
	    if (!evector_type_p(v->t))
		evm_raise(except_not_a_number);
	    evector_pow_mpr(v->v.o, r0.v.mpr, v->t & ~(t_const|t_vector));
	    r0.v.o = obj;
	    r0.t = eobject_type(obj);
	    break;
    }
}

void
evm_r_hypot(evalue_t *v)
{
    switch (v->t) {
	case t_void:
	    er_abs_r(thr_rr, r0.v.mpr);
	    break;
	case t_int:
	    er_hypot_i_r(thr_rr, v->v.i, r0.v.mpr);
	    break;
	case t_float:
	    er_hypot_d_r(thr_rr, v->v.d, r0.v.mpr);
	    break;
	case t_mpz:		case t_shz:
	    er_hypot_z_r(thr_rr, v->v.mpz, r0.v.mpr);
	    break;
	case t_mpq:		case t_shq:
	    er_hypot_q_r(thr_rr, v->v.mpq, r0.v.mpr);
	    break;
	case t_mpr:		case t_shr:
	    er_hypot_r_r(thr_rr, v->v.mpr, r0.v.mpr);
	    break;
	case t_cdd:
	    er_hypot_dd_r(thr_rr, v->v.cdd, r0.v.mpr);
	    break;
	case t_cqq:		case t_sqq:
	    er_hypot_qq_r(thr_rr, v->v.cqq, r0.v.mpr);
	    break;
	case t_mpc:		case t_shc:
	    /* sqrt(a*(conj a) + b*b) */
	    er_hypot_cc_r(thr_rr, v->v.mpc, r0.v.mpr);
	    break;
	default:
	    if (!evector_type_p(v->t))
		evm_raise(except_not_a_number);
	    evector_hypot_mpr(v->v.o, r0.v.mpr, v->t & ~(t_const|t_vector));
	    r0.v.o = obj;
	    r0.t = eobject_type(obj);
	    return;
    }
    chk_rr();
}

void
evm_r_complex(evalue_t *v)
{
    if (likely(!mpfr_zero_p(r0.v.mpr))) {
	switch (v->t) {
	    case t_void:
		mpfr_set(thr_ri, r0.v.mpr, thr_rnd);
		mpfr_set_ui(thr_rr, 0, thr_rnd);
		set_rr_chk_rrr();
		break;
	    case t_int:
		mpfr_set(thr_ri, r0.v.mpr, thr_rnd);
		empr_set_i(thr_rr, v->v.i);
		set_rr_chk_rrr();
		break;
	    case t_float:
		mpfr_set(thr_ri, r0.v.mpr, thr_rnd);
		mpfr_set_d(thr_rr, v->v.d, thr_rnd);
		set_rr_chk_rrr();
		break;
	    case t_mpz:		case t_shz:
		mpfr_set(thr_ri, r0.v.mpr, thr_rnd);
		mpfr_set_z(thr_rr, v->v.mpz, thr_rnd);
		set_rr_chk_rrr();
		break;
	    case t_mpq:		case t_shq:
		mpfr_set(thr_ri, r0.v.mpr, thr_rnd);
		mpfr_set_q(thr_rr, v->v.mpq, thr_rnd);
		set_rr_chk_rrr();
		break;
	    case t_mpr:		case t_shr:
#if 1
		mpfr_set_fr(thr_ri, r0.v.mpr, thr_rnd);
		mpfr_set_fr(thr_rr, v->v.mpr, thr_rnd);
#else
		/* this is wrong if r0.v.mpr == thr_rr,
		 * because mpc would first assign v->v.mpr to
		 * mpc_realref(thr_c) before setting mpc_imagref(thr_c),
		 * what would cause it to modify r0.v.mpr before
		 * actually using it's value */
		mpc_set_fr_fr(thr_c, v->v.mpr, r0.v.mpr, thr_rndc);
#endif
		set_rr_chk_rrr();
		break;
	    default:
		evm_raise(except_not_a_real_number);
	}
    }
    else
	evm_v_complex(v);
}
